package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "站内信接口")
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    // 注⼊业务层
    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;

    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send (HttpServletRequest request,
                           @ApiParam(value = "接收用户Id") @RequestParam(value = "receiveUserId") @NonNull Long receiveUserId,
                           @ApiParam(value = "站内信内容") @RequestParam(value = "content") @NonNull String content){
        //获取发送用户信息
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //判断用户是否被禁言
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发送
        if(user.getId()==receiveUserId){
            return AppResult.failed("不能给自己发送站内信");
        }
        // 查询接收用户
        User receiveUser = userService.selectById(receiveUserId);
        // ⽬标用户不存在
        if(receiveUser==null || receiveUser.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        // 构造对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        // 调⽤业务层
        messageService.create(message);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation(("获取未读消息个数"))
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        //获取发送用户信息
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //调用service
        Integer result = messageService.selectUnreadCount(user.getId());
        //返回结果
        return AppResult.success(result);
    }

    @ApiOperation("查询用户所有站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        //获取用户信息
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //调用service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        //返回结果
        return AppResult.success(messages);
    }

    @ApiOperation("更新为已读")
    @PostMapping ("/markRead")
    public AppResult<Message> markRead(HttpServletRequest request,
                                       @ApiParam("站内信Id") @RequestParam("id") @NonNull Long id){
        //根据id查询内容
        Message message = messageService.selectById(id);
        // 2. 站内信是否存在
        if (message == null || message.getDeleteState() == 1) {
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //获取用户信息
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //判断接收方是否为自己
        if(message.getReceiveUserId()!= user.getId()){
            return AppResult.failed("接收了不属于自己的信息");
        }
        // 更新为已读状态
        messageService.updateStateById(message.getId(), (byte) 1);
        // 返回结果
        return AppResult.success(message);
    }

    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public AppResult reply (HttpServletRequest request,
                            @ApiParam("要回复的站内信Id") @RequestParam("repliedId") @NonNull Long repliedId,
                            @ApiParam("站内信的内容") @RequestParam("content") @NonNull String content) {
        //获取用户信息
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //校验用户状态
        if(user.getState()==1){
            //禁言状态
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 校验要回复的站内信状态
        Message existsMessage = messageService.selectById(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        // 不能给自己回复
        if (user.getId() == existsMessage.getPostUserId()) {
            // 返回错误描述
            return AppResult.failed("不能回复自己的站内信");
        }
        // 构造对象
        Message message = new Message();
        message.setPostUserId(user.getId()); // 发送者
        message.setReceiveUserId(existsMessage.getPostUserId()); // 接收者
        message.setContent(content); // 内容
        // 调用Service
        messageService.reply(repliedId, message);
        // 返回结果
        return AppResult.success();
    }
}
